﻿using Core.Domain.Model;
using NSBus.Dto.Commands;
using Sanelib.Common.Helpers;
using Sanelib.DataOperation;

namespace NSBus.Server.CommandHandlers
{
    public class AddLeadLetterHandler : MessageHandler<AddLeadCommand>
    {
        private readonly IRepository<Lead> _leadRepository;
        private readonly IRepository<Member> _memberRepository;

        public AddLeadLetterHandler(IUnitOfWork unitOfWork, IRepository<Lead> leadRepository, IRepository<Member> memberRepository)
            : base(unitOfWork)
        {
            _leadRepository = leadRepository;
            _memberRepository = memberRepository;
        }

        public override void HandleMessage(AddLeadCommand command)
        {
            var serverhost = _memberRepository.GetByKey(Property.Of<Member>(x => x.Code), "serverhost");

            var newsLetter = new Lead
            {
                Id = GuidComb.New(),
                Name = command.Name,
                Email = command.Email,
                Phone = command.Phone,
                CompanyName = command.CompanyName,
                Description = command.Description,
                CreatedBy = serverhost.Id,
                CreatedOn = command.CreatedOn
            };
            _leadRepository.Add(newsLetter);
        }
    }
}

